﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsuranceSell.Code.Services;
using InsuranceSell.Code.Data;

namespace InsuranceSell
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        public void Login() {
            string Username = txtUsername.Text.Trim();
            string Password = txtPassword.Text.Trim();

            DataTable userInfo = UserServices.CheckUserLogin(Username, Encryptor.HashMD5(Password));
            string helloString = string.Empty;
            bool isAdmin = false;

            if (userInfo == null || userInfo.Rows.Count == 0)
            {
                helloString = string.Empty;
                MessageBox.Show("Đăng Nhập Không thành công!", "Đăng Nhập");
            }
            else
            {
                helloString = userInfo.Rows[0][0].ToString();
                isAdmin = bool.Parse(userInfo.Rows[0][1].ToString());
                frmMain mainForm = new frmMain();
                mainForm.strFullName = helloString;
                mainForm.isAdmin = isAdmin;
                this.Hide();
                mainForm.ShowDialog();
                this.Close();
            }


            //PassData passer = new PassData(mainForm.ReceiveData);
            //passer(helloString);    
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                Login();
            }
        }
    }
}
